/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.actions.becario;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.uca.sisbec.commons.HibernateAction;
import com.uca.sisbec.forms.becario.PrestamoLibroForm;
import com.uca.sisbec.helpers.manto.LibroHelper;
import com.uca.sisbec.mapping.Becario;
/**
 *
 * @author Jorgore
 */
public class DevolucionLibroAction extends HibernateAction{
    
    public ActionForward init(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception{
        if (!isBecarioInSession(request)) return mapping.findForward("searchBecario");
        request.setAttribute("devolver", "N");
        setOptionsList(request);
        return mapping.findForward("success");
    }
    
    public ActionForward cargarLibro(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception{
        PrestamoLibroForm plf = (PrestamoLibroForm) form;
        LibroHelper helper = new LibroHelper(getSession(request));
        helper.cargarLibroDevForm(plf);
        request.setAttribute("devolver", "Y");
        setOptionsList(request);
        return mapping.findForward("success");
    }
    
    public ActionForward devolverLibro(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception{
        PrestamoLibroForm plf = (PrestamoLibroForm) form;
        LibroHelper helper = new LibroHelper(getSession(request));
        helper.devolverLibro(plf);
        plf.setPrestamo(0);
        plf.setFecDevolucion("");
        plf.setIsbn("");
        plf.setLibro("");
        request.setAttribute("devolver", "N");
        setOptionsList(request);
        return mapping.findForward("success");
    }
    
    public void setOptionsList(HttpServletRequest request){
        LibroHelper helper = new LibroHelper(getSession(request));
        Becario becario = getBecarioInSession(request);
        String carnet = becario.getCarnet();
        request.setAttribute("prestamoList", helper.getLibrosPrestados(carnet));
    }
}
